//
//  adviceViewController.m
//  忘不了
//
//  Created by rimi on 16/7/19.
//  Copyright © 2016年 handuan. All rights reserved.
//

#import "adviceViewController.h"
#import "SDAutoLayout.h"
#import "BaseRequest+MoneyAndRemember.h"
#import "UserModel.h"
#import "CheckWeb.h"
@interface adviceViewController ()
@property (nonatomic,strong)UILabel *label;
@property (nonatomic,strong)UITextView *contentView;
@property (nonatomic,strong)UIButton *sendButton;
@end

@implementation adviceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"用户反馈";
    
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                         forBarMetrics:UIBarMetricsDefault];//隐藏导航栏返回按钮自带的文字
    //设置导航栏标题颜色、字体大小等
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor brownColor]}];
    
    UIImageView *backGroudView = [[UIImageView alloc]initWithFrame:self.view.frame];
    backGroudView.image = [UIImage imageNamed:@"s.jpg"];
    [self.view addSubview:backGroudView];
    
    [self loadUI];
    
}

- (void)loadUI {
    
    _label = [[UILabel alloc]init];
    _label.textColor = [UIColor blackColor];
    _label.text = @"请对我们说点什么：";
    _label.textAlignment = NSTextAlignmentLeft;
    _label.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:_label];
    _label.sd_layout
    .leftSpaceToView(self.view,20)
    .rightSpaceToView(self.view,20)
    .topSpaceToView(self.view,100)
    .heightIs(self.view.frame.size.width*0.1);
    
    _contentView = [[UITextView alloc]init];
    _contentView.backgroundColor = [UIColor whiteColor];
    _contentView.textAlignment = NSTextAlignmentLeft;
    _contentView.textColor = [UIColor blackColor];
    _contentView.font = [UIFont systemFontOfSize:18];
    _contentView.layer.cornerRadius = 10;
    _contentView.layer.masksToBounds = YES;
    [self.view addSubview:_contentView];
    _contentView.sd_layout
    .leftSpaceToView(self.view,20)
    .rightSpaceToView(self.view,20)
    .topSpaceToView(_label,10)
    .heightIs(self.view.frame.size.width*0.5);
    
    _sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _sendButton.backgroundColor = [UIColor colorWithRed:0.851 green:0.8118 blue:0.7255 alpha:1.0];
    [_sendButton setTitle:@"发送" forState:UIControlStateNormal];
    [_sendButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
    _sendButton.layer.cornerRadius = 10;
    _sendButton.layer.masksToBounds = YES;
    [_sendButton addTarget:self action:@selector(sendButtonTaped) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_sendButton];
    _sendButton.sd_layout
    .centerXIs(self.view.frame.size.width/2)
    .topSpaceToView(_contentView,100)
    .widthIs(self.view.frame.size.width*0.2)
    .heightIs(self.view.frame.size.width*0.1);
    
}

- (void)sendButtonTaped {
    if ([_contentView.text isEqual:@""]) {
        UIAlertController *tips = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请填写您想对我们说的话" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [tips addAction:sure];
        [self presentViewController:tips animated:YES completion:nil];
    } else {
        BOOL isWeb = [CheckWeb checkWang];
        if (isWeb == NO) {
            UIAlertController *tips = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请检查网络" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            [tips addAction:sure];
            [self presentViewController:tips animated:YES completion:nil];
        }else{
            [BaseRequest getAdviceFromUserName:[UserModel shareUserModel].userNmae withDetail:_contentView.text completionhanlder:^(Response *response) {
                if (response.isSuccess) {
                    UIAlertController *tips = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"反馈成功，谢谢您的宝贵意见" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        [self.navigationController popViewControllerAnimated:YES];
                    }];
                    [tips addAction:sure];
                    [self presentViewController:tips animated:YES completion:nil];
                }
            }] ;

        }
        
        
        
    }
}

//点空白回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


@end
